package assetsmanagement

import (
	"fmt"
	"k8s-admin/global"
	"k8s-admin/models"
	"k8s-admin/utils"
)

type InterfaceAssetsManagement interface {
	Create(host, username, password, assets_name, assets_class string, port, status *int) error
	List(limit, page int) (*models.AssetsManagementList, error)
	Delete(assets_uuid string) error
	Update(assets_uuid, host, username, password, assets_name, assets_class string, port, status *int) error
	GETHOSTCPUInfo(assets_uuid string) ([]models.AssetsManagementInfo, error)

}

type NewInterfaceAssetsManagement struct{}

func NewAssetsManagement() InterfaceAssetsManagement {
	return &NewInterfaceAssetsManagement{}
}
func (a *NewInterfaceAssetsManagement) Create(host, username, password, assets_name, assets_class string, port, status *int) error {
	err := global.GORM.Create(&models.AssetsManagement{
		AssetsUUID:  utils.GemSnowFlask(assets_name),
		Host:        host,
		Password:    password,
		Username:    username,
		AssetsName:  assets_name,
		Port:        port,
		Status:      status,
		AssetsClass: assets_class,
	}).Error
	if err != nil {
		fmt.Printf("err: %v\n", err)
		return err
	} else {
		return nil
	}
}

func (a *NewInterfaceAssetsManagement) List(limit, page int) (*models.AssetsManagementList, error) {
	list := []models.AssetsManagement{}
	var Count int64
	tx := global.GORM.Offset((page - 1) * limit).Limit(limit).Find(&list).Count(&Count)
	if tx.Error != nil {
		return nil, tx.Error
	} else {

		return &models.AssetsManagementList{
			Items: list,
			Total: Count,
		}, nil
	}
}

/*删除接口*/
func (a *NewInterfaceAssetsManagement) Delete(assets_uuid string) error {
	tx := global.GORM.Where("assets_uuid = ?", assets_uuid).Delete(&models.AssetsManagement{})
	if tx.Error != nil {
		return tx.Error
	} else {
		return nil
	}
}

/*更新*/
func (a *NewInterfaceAssetsManagement) Update(assets_uuid, host, username, password, assets_name, assets_class string, port, status *int) error {
	tx := global.GORM.Model(&models.AssetsManagement{}).Where("assets_uuid = ?", assets_uuid).Updates(&models.AssetsManagement{
		Host:        host,
		Password:    password,
		Username:    username,
		AssetsName:  assets_name,
		Port:        port,
		Status:      status,
		AssetsClass: assets_class,
	})
	if tx.Error != nil {
		return tx.Error
	} else {
		return nil
	}
}



func (a *NewInterfaceAssetsManagement) GETHOSTCPUInfo(assets_uuid string) ([]models.AssetsManagementInfo, error) {
	var tmp_host_info models.AssetsManagement
	err := global.GORM.Where("assets_uuid = ?", assets_uuid).First(&tmp_host_info).Error
	if err != nil {
		return nil, err
	} else {
		// 查询到主机的uuid后，再通过主机的uuid去查询主机的cpu信息表，就算主机的IP变了也不会影响到查询结果
		var tmp_cpu_info []models.AssetsManagementInfo
		err := global.GORM.Where("assets_management_uuid = ?", tmp_host_info.AssetsUUID).Find(&tmp_cpu_info).Error
		if err != nil {
			return nil, err
		} else {
			return tmp_cpu_info, nil
		}

	}
}


/*模拟数据代码*/
func CrontabCPUInfo() error {
	var assets_info []models.AssetsManagement
	tx := global.GORM.Find(&assets_info)
	if tx.Error != nil {
		return tx.Error
	} else {
		infoList, err := utils.CPUTimes()
		if err != nil {
			return err
		} else {
			for _, item := range infoList {
				err = global.GORM.Create(&models.AssetsManagementInfo{
					AssetsManagementUUID: assets_info[0].AssetsUUID,
					// CPU:        item.CPU,
					User:   item.User,
					System: item.System,
					Idle:   item.Idle,
					Nice:   item.Nice,
					Iowait: item.Iowait,
				}).Error
				if err != nil {
					return err
				}

			}
			return nil

		}
	}
}